package com.hqyj.crmdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.crmdemo.annotation.Auth;
import com.hqyj.crmdemo.entity.DevPlan;
import com.hqyj.crmdemo.entity.RestResult;
import com.hqyj.crmdemo.entity.SaleChance;
import com.hqyj.crmdemo.service.IDevPlanService;
import com.hqyj.crmdemo.service.ISaleChanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 何万林
 * @since 2022-10-17
 */
@RestController
@RequestMapping("/saleChance")
@CrossOrigin
public class SaleChanceController {

    @Autowired
    ISaleChanceService service;
    @Autowired
    IDevPlanService devPlanService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public RestResult<SaleChance> queryAll(SaleChance chance) {
        List<SaleChance> chances = service.queryAll();
        return RestResult.ok(chances);
    }

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResult<SaleChance> queryPage(@RequestParam(defaultValue = "1")@PathVariable("page") Integer page,
                                            @RequestParam(defaultValue = "7")@PathVariable("size") Integer size){
        Page<SaleChance> page1 = service.page(new Page<>(page, size));
        Long total = page1.getTotal();

        return  RestResult.ok(page1.getRecords(),total);
    }

    @RequestMapping(value = "/selectOne", method = RequestMethod.POST)
    public RestResult<SaleChance> selectOne(Integer saleId) {
        System.out.println(saleId+"-------------------------------------");
        QueryWrapper<SaleChance> wrapper = new QueryWrapper<>();
        wrapper.eq("sale_id",saleId);
        List<SaleChance> chances = service.queryOne(wrapper);
        return RestResult.ok(chances);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public RestResult<SaleChance> add(SaleChance chance) {
        chance.setSaleCreate("测试");
        chance.setSaleCreatedate(LocalDate.now());
        Boolean aBoolean = service.add(chance);
        if (aBoolean) {
            return RestResult.ok("success");
        }else{
            return RestResult.ok("false");
        }
    }

    /**
     * 修改销售机会
     * @param chance
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public RestResult<SaleChance> update(SaleChance chance){
        Boolean update = service.update(chance);
        if (update) {
            return RestResult.ok("success");
        }else{
            return RestResult.ok("false");
        }
    }

    /**
     * 指派销售机会
     * @param chance
     * @return
     */
    @RequestMapping(value = "/updateRea",method = RequestMethod.POST)
    public RestResult<SaleChance> updateRea(SaleChance chance){
        //指派时间，同时修销售机会状态为”已分配“
        chance.setSalaDesignatedate(LocalDate.now());
        chance.setSalaState("已分配");
        //同时将指派人和销售机会编号存入表dev_plan
        DevPlan devPlan = new DevPlan();
        devPlan.setSaleId(chance.getSaleId());
        devPlan.setuName(chance.getSalaDesignate());
        devPlanService.addDevPlan(devPlan);
        Boolean update = service.update(chance);
        if (update) {
            return RestResult.ok("success");
        }else{
            return RestResult.ok("false");
        }
    }


    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public RestResult<SaleChance> delete(Integer saleId ){
        Boolean delete = service.delete(saleId);
        if (delete) {
            return RestResult.ok("success");
        }else{
            return RestResult.ok("false");
        }
    }

    @RequestMapping(value = "/test1")
    @Auth(role = "1")
    public String test1(){
        return "角色ID:1";
    }

    @RequestMapping(value = "/test2")
    @Auth(role = "2")
    public String test2(){
        return "角色ID:2";
    }

    @RequestMapping(value = "/test3")
    @Auth(role = "1,2")
    public String test3(){
        return "角色ID:1、2";
    }

    @RequestMapping(value = "/test4")
    public String test4(){
        return "所有都能";
    }
}
